using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MultiVMR9cs;

namespace Sample
{
    public partial class Form1 : Form
    {
        private VideoOutputForm _videoWindow;

        public Form1()
        {
            InitializeComponent();
            // If LoaderLock was detected:
            // Go to Debug/Exceptions. Open up the Managed Debugging Assistants node 
            // on the tree and scroll down till you find LoaderLock and uncheck it.

            _videoWindow = new VideoOutputForm();
            _videoWindow.Show(this);
        }

        private void _addSourceButton_Click(object sender, EventArgs e)
        {
            // Add a video source and it's VideoControlPanel

            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                VideoSource source = null;

                try
                {
                    source = new VideoSource(_videoWindow.MultiVMR9Panel.Renderer, ofd.FileName,true);
                    _videoWindow.MultiVMR9Panel.Renderer.AddSource(source);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error loading video");
                }

                if (source != null)
                {
                    // create the panel and move it to the bottom of the controls
                    VideoControlPanel vp = new VideoControlPanel(source, Path.GetFileName(ofd.FileName));
                    vp.closeButton.Click += new EventHandler(closeButton_Click);
                    _sourcesPanel.Controls.Add(vp);
                    _sourcesPanel.Controls.SetChildIndex(vp, 0);
                    vp.CloneSource += new EventHandler(vp_CloneSource);
                }
            }
            ofd.Dispose();
            ofd = null;
        }

        private void imageSourceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Add an image source and it's VideoControlPanel

            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                ImageSource source = null;

                try
                {
                    source = new ImageSource(_videoWindow.MultiVMR9Panel.Renderer, ofd.FileName);
                    _videoWindow.MultiVMR9Panel.Renderer.AddSource(source);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error loading image");
                }

                if (source != null)
                {
                    // create the panel and move it to the bottom of the controls
                    VideoControlPanel vp = new VideoControlPanel(source, Path.GetFileName(ofd.FileName));
                    vp.closeButton.Click += new EventHandler(closeButton_Click);
                    _sourcesPanel.Controls.Add(vp);
                    _sourcesPanel.Controls.SetChildIndex(vp, 0);
                    vp.CloneSource += new EventHandler(vp_CloneSource);
                }
            }
            ofd.Dispose();
            ofd = null;
        }

        void vp_CloneSource(object sender, EventArgs e)
        {
            // clone a source

            CloneSource source = null;
            VideoSource vs = (sender as VideoControlPanel).VideoSource;

            try
            {
                source = new CloneSource(_videoWindow.MultiVMR9Panel.Renderer, vs);
                _videoWindow.MultiVMR9Panel.Renderer.AddSource(source);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error cloning source");
            }

            if (source != null)
            {
                // create the panel and move it to the bottom of the controls
                VideoControlPanel vp = new VideoControlPanel(source, "Clone of " + (sender as VideoControlPanel).Caption);
                vp.closeButton.Click += new EventHandler(closeButton_Click);
                _sourcesPanel.Controls.Add(vp);
                _sourcesPanel.Controls.SetChildIndex(vp, 0);
                vp.CloneSource += new EventHandler(vp_CloneSource);
            }
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            CloseVideoControlPanel((VideoControlPanel)(sender as SimpleImageButton).Tag);
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            // remove all the sources before closing
            while (_sourcesPanel.Controls.Count != 0)
            {
                CloseVideoControlPanel(_sourcesPanel.Controls[0] as VideoControlPanel);
            }

            base.OnFormClosing(e);
        }

        private void CloseVideoControlPanel(VideoControlPanel videoPanel)
        {
            // remove any clones first
            foreach (VideoControlPanel vp in GetClonePanels(videoPanel))
            {
                _sourcesPanel.Controls.Remove(vp);
                _videoWindow.MultiVMR9Panel.Renderer.RemoveSource(vp.VideoSource);
                vp.VideoSource.Dispose();
            }

            // remove the given videoPanel
            _sourcesPanel.Controls.Remove(videoPanel);
            _videoWindow.MultiVMR9Panel.Renderer.RemoveSource(videoPanel.VideoSource);
            videoPanel.VideoSource.Dispose();
        }

        /// <summary>
        /// Get a list of VideoControlPanels that are clones of the given VideoControlPanel
        /// </summary>
        /// <param name="vp"></param>
        /// <returns></returns>
        private List<VideoControlPanel> GetClonePanels(VideoControlPanel videoPanel)
        {
            List<CloneSource> clones = _videoWindow.MultiVMR9Panel.Renderer.GetClones(videoPanel.VideoSource);
            List<VideoControlPanel> panels = new List<VideoControlPanel>();

            // find the panel for each clone source
            foreach (CloneSource clone in clones)
            {
                VideoControlPanel foundPanel = null;
                foreach (Control c in _sourcesPanel.Controls)
                {
                    if (c is VideoControlPanel)
                    {
                        VideoControlPanel vp = c as VideoControlPanel;
                        if (vp.VideoSource == clone)
                        {
                            foundPanel = vp;
                            break;
                        }
                    }
                }

                if (foundPanel != null)
                {
                    panels.Add(foundPanel);
                }
            }

            return panels;
        }

        private void _scrollablePanel_Resize(object sender, EventArgs e)
        {
            // adjust the size of the sources panel when the form resizes
            _sourcesPanel.Width = _scrollablePanel.ClientSize.Width;
        }

        private void _sourcesPanel_ControlAddedRemoved(object sender, ControlEventArgs e)
        {
            // adjust the size of the sources panel when VideoControlPanels are added/removed
            _sourcesPanel.Width = _scrollablePanel.ClientSize.Width;
            _sourcesPanel.Height = _sourcesPanel.Controls.Count * 126;
        }

        private void _masterVolumeTrackBar_ValueChanged(object sender, EventArgs e)
        {
            if (_masterVolumeTrackBar.Tracking)
            {
                _videoWindow.MultiVMR9Panel.Renderer.MasterVolume = (float)_masterVolumeTrackBar.Value / 1000f;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _videoWindow.MultiVMR9Panel.FullScreen = _videoWindow.MultiVMR9Panel.FullScreen == FullScreenMode.None ?
                FullScreenMode.FullScreen : FullScreenMode.None;
        }

        private void _addSourceButton_Click_1(object sender, EventArgs e)
        {
            _sourceContextMenuStrip.Show(_addSourceButton, new Point(0, _addSourceButton.Height));
        }
    }
}